package log

import (
	"fmt"
	"github.com/coreos/capnslog"
	"github.com/natefinch/lumberjack"
	"log"
	"path/filepath"
)

var fileLogger *log.Logger

type Overlog struct {
	Logger *capnslog.PackageLogger
	Repo   string
	Pkg    string
}

func NewOverlog(repo string, pkg string) (log *Overlog) {
	var caplog = capnslog.NewPackageLogger(repo, pkg)
	//	var overlog = &Overlog{caplog, repo, pkg}
	return &Overlog{caplog, repo, pkg}
}

func Init(log_path string, logFileName string) {
	flags := int(log.Ldate | log.Ltime | log.Lmicroseconds)
	rotate_file := &lumberjack.Logger{
		Filename:   filepath.Join(log_path, fmt.Sprintf("%v.log", logFileName)),
		MaxSize:    100,
		MaxBackups: 20,
		MaxAge:     1,
		LocalTime:  true,
	}
	fileLogger = log.New(rotate_file, "", flags)
}

func (lv *Overlog) Log2File(logLevel string, args ...interface{}) {
	fileLogger.Output(2, fmt.Sprintf(lv.Repo+" "+lv.Pkg+" "+logLevel+" %v \r\n", args...))
	//	fileLogger.Output(2, fmt.Sprintf(" %v %v %v %v \r\n", lv.Repo, lv.Pkg,logLevel,args...))
}

func (lv *Overlog) Log2Filef(logLevel string, format string, args ...interface{}) {
	f := fmt.Sprintf(lv.Repo+" "+lv.Pkg+" "+logLevel+" %v \r\n", format)
	fileLogger.Output(2, fmt.Sprintf(f, args...))
}

func (l *Overlog) Flush() {
	l.Logger.Flush()
}

func (l *Overlog) Log(lv capnslog.LogLevel, args ...interface{}) {
	l.Log2File(lv.String(), args...)
	l.Logger.Log(lv, args...)
}

func (l *Overlog) Logf(lv capnslog.LogLevel, format string, args ...interface{}) {
	l.Log2Filef(lv.String(), format, args...)
	l.Logger.Logf(lv, format, args...)
}

//func  (l *Overlog) Log2File(logLevel string, args ...interface{}){
//	f1 := fmt.Sprint(args)
//	f := fmt.Sprintf(" proxy %s  %s \r\n", logLevel, f1)
//	fileLogger.Output(2, f)
//}

func (l *Overlog) Debug(entries ...interface{}) {
	l.Log2File("Debug", entries...)

	l.Logger.Debug(entries...)
}

func (l *Overlog) Debugf(format string, args ...interface{}) {
	l.Log2Filef("Debug", format, args...)

	l.Logger.Debugf(format, args...)
}

func (l *Overlog) Error(entries ...interface{}) {
	l.Log2File("Error", entries...)

	l.Logger.Error(entries...)
}

func (l *Overlog) Errorf(format string, args ...interface{}) {
	l.Log2Filef("Error", format, args...)
	l.Logger.Errorf(format, args...)
}

func (l *Overlog) Fatal(entries ...interface{}) {
	l.Log2File("Fatal", entries...)
	l.Logger.Fatal(entries...)
}

func (l *Overlog) Fatalf(format string, args ...interface{}) {
	l.Log2Filef("Fatal", format, args...)

	l.Logger.Fatalf(format, args...)
}

func (l *Overlog) Fatalln(args ...interface{}) {
	l.Log2File("Fatal", args...)

	l.Logger.Fatalln(args...)
}

func (l *Overlog) Info(entries ...interface{}) {
	l.Log2File("Info", entries...)

	l.Logger.Info(entries...)
}

func (l *Overlog) Infof(format string, args ...interface{}) {
	l.Log2Filef("Info", format, args...)

	l.Logger.Infof(format, args...)
}

func (l *Overlog) Notice(entries ...interface{}) {
	l.Log2File("Notice", entries...)

	l.Logger.Notice(entries...)
}

func (l *Overlog) Noticef(format string, args ...interface{}) {
	l.Log2Filef("Notice", format, args...)

	l.Logger.Noticef(format, args...)
}

func (l *Overlog) Panic(args ...interface{}) {
	l.Log2File("Panic", args...)

	l.Logger.Panic(args...)
}

func (l *Overlog) Panicf(format string, args ...interface{}) {
	l.Log2Filef("Panic", format, args...)

	l.Logger.Panicf(format, args...)
}

func (l *Overlog) Print(args ...interface{}) {
	l.Log2File("Print", args...)

	l.Logger.Print(args...)
}

func (l *Overlog) Printf(format string, args ...interface{}) {
	l.Log2Filef("Debug", format, args...)

	l.Logger.Printf(format, args...)
}

func (l *Overlog) Println(args ...interface{}) {
	l.Log2File("Println", args...)

	l.Logger.Println(args...)
}

func (l *Overlog) Trace(entries ...interface{}) {
	l.Log2File("Trace", entries...)

	l.Logger.Trace(entries...)

}
func (l *Overlog) Tracef(format string, args ...interface{}) {
	l.Log2Filef("Trace", format, args...)

	l.Logger.Tracef(format, args...)

}
func (l *Overlog) Warning(entries ...interface{}) {
	l.Log2File("Warning", entries...)

	l.Logger.Warning(entries...)
}

func (l *Overlog) Warningf(format string, args ...interface{}) {
	l.Log2Filef("Warning", format, args...)

	l.Logger.Warningf(format, args...)
}
